<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>模板</title>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../js/react-dom.development.js"
    ></script>
    <!-- 引入babel，用于将jsx转换成js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/babel">
      // 受控组件：变量由维护在状态中state，随着改变而改变，使用也从state中获取
      class Login extends React.Component {
        state = {
          username: "",
          password: "",
        };

        handleSubmit = (event) => {
          console.log(this);
          event.preventDefault();
          const { username, password } = this.state;
          alert(`username:${username}----password:${password}`);
        };

        saveUsername = (event) => {
          this.setState({
            username: event.target.value,
          });
        };

        savePassword = (event) => {
          this.setState({
            password: event.target.value,
          });
        };

        render() {
          return (
            <form action="#" onSubmit={this.handleSubmit}>
              用户名：
              <input
                onChange={this.saveUsername}
                type="text"
                name="username"
                placeholder="用户名"
              />
              密码：
              <input
                onChange={this.savePassword}
                type="password"
                name="password"
              />
              <button>登录</button>
            </form>
          );
        }
      }

      ReactDOM.render(<Login />, document.querySelector("#test"));
    </script>
  </body>
</html>
